package com.yunxin.core.infra.persistence.repository.channel;

import com.yunxin.core.domain.model.channel.ChannelPromotion;
import com.yunxin.core.domain.model.channel.ChannelPromotionRepository;
import com.yunxin.core.infra.persistence.converter.ChannelConvertor;
import com.yunxin.core.infra.persistence.entity.channel.ChannelPromotionDO;
import com.yunxin.core.infra.persistence.entity.channel.query.result.ChannelPromotionSaveResult;
import com.yunxin.core.infra.persistence.mapper.channel.ChannelPromotionExtendMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.Objects;

/**
 * @author zero°
 * @since 2023-07-12
 */
@Repository
@RequiredArgsConstructor
public class ChannelPromotionRepositoryImpl implements ChannelPromotionRepository {

    private final ChannelPromotionExtendMapper channelPromotionExtendMapper;

    private final ChannelConvertor channelConvertor;

    @Override
    public boolean saveChannelPromotion(ChannelPromotion channelPromotion) {
        ChannelPromotionDO channelPromotionDO = channelConvertor.formChannelPromotion(channelPromotion);
        int result = channelPromotionExtendMapper.insertSelective(channelPromotionDO);
        channelPromotion.setId(channelPromotionDO.getId());
        return result > 0;
    }

    @Override
    public boolean updateChannelPromotion(ChannelPromotion channelPromotion) {
        ChannelPromotionDO channelPromotionDO = channelConvertor.formChannelPromotion(channelPromotion);
        int result = channelPromotionExtendMapper.updateByPrimaryKeySelective(channelPromotionDO);
        return result > 0;
    }

    @Override
    public ChannelPromotion getChannelPromotionById(Long id) {
        ChannelPromotionDO channelPromotionDO = channelPromotionExtendMapper.selectByPrimaryKey(id);
        return channelConvertor.toChannelPromotion(channelPromotionDO);
    }

    @Override
    public boolean getChannelPromotionSaveResult(Long id) {
        ChannelPromotionSaveResult result = channelPromotionExtendMapper.getChannelPromotionSaveResult(id);
        return Objects.isNull(result.getChannelPromotionLinkId()) || Objects.isNull(result.getChannelPromotionUnitPriceId());
    }


}
